﻿#include "EditorMainWindowViewModel.h"

#include "Framework/Editor.h"

#include "EditorMainWindowModel.h"
#include "Widgets/Window.h"

#include "Asset/EngineDefaultPackage.h"
#include "Asset/EditorDefaultPackage.h"

#include "Ability/Explorer/EditorExplorerAbilityGroupModel.h"
#include "Ability/Explorer/EditorExplorerAbilityGroupViewModel.h"
#include "Ability/DebugAndRun/EditorDebugAndRunAbilityGroupModel.h"
#include "Ability/DebugAndRun/EditorDebugAndRunAbilityGroupViewModel.h"

#include "PageArea/EditorPageAreaManagerViewModel.h"

#include "AbilityGroup/SideBar/EditorSideBarViewModel.h"
#include "AbilityGroup/SideBar/EditorSideBarModel.h"

#include "AbilityGroup/Panel/EditorPanelModel.h"
#include "AbilityGroup/Panel/EditorPanelViewModel.h"

#include "StatusBar/EditorStatusBarModel.h"
#include "StatusBar/EditorStatusBarViewModel.h"

#include "Notification/EditorNotificationModel.h"
#include "Notification/EditorNotificationViewModel.h"


#include "Menubar/EditorMenuBarViewModel.h"

#include "Widgets/Splitable/EditorSplitableLayoutViewModel.h"

REditorMainWindowViewModel::REditorMainWindowViewModel()
{
}

void REditorMainWindowViewModel::BindModel(TSharedObjectPtr<REditorWindowModel> InModel)
{
    Super::BindModel(InModel);
    Model = InModel;



    MainSideBarToggleButtonImage_Show=REditorDefaultPackage::Get()->GetIcon(U"CodiconLayoutSidebarLeft");
    MainSideBarToggleButtonImage_Hide=REditorDefaultPackage::Get()->GetIcon(U"CodiconLayoutSidebarLeftOff");
    PanelToggleButtonImage_Show=REditorDefaultPackage::Get()->GetIcon(U"CodiconLayoutPanel");
    PanelToggleButtonImage_Hide=REditorDefaultPackage::Get()->GetIcon(U"CodiconLayoutPanelOff");
    SecondarySideBarToggleButtonImage_Show=REditorDefaultPackage::Get()->GetIcon(U"CodiconLayoutSidebarRight");
    SecondarySideBarToggleButtonImage_Hide=REditorDefaultPackage::Get()->GetIcon(U"CodiconLayoutSidebarRightOff");


    MainSideBar=NewObject<REditorSideBarViewModel>(this);
    MainSideBar->BindModel(Model->GetMainSideBar());

    SecondarySideBar=NewObject<REditorSideBarViewModel>(this);
    SecondarySideBar->BindModel(Model->GetSecondarySideBar());

    PanelViewModel=NewObject<REditorPanelViewModel>(this);
    PanelViewModel->BindModel(Model->GetPanel());


    BodyLayout=NewObject<REditorSplitableLayoutViewModel>(this);
    BodyLayout->OnBindItemViewModel.BindLambda(
        [this](RObjectPtr InItemModel)->RObjectPtr
        {
            if(InItemModel==Model->GetMainSideBar())
            {
                return MainSideBar;
            }
            else if(InItemModel==Model->GetSecondarySideBar())
            {
                return SecondarySideBar;
            }
            else if(InItemModel==Model->GetPanel())
            {
                return PanelViewModel;
            }
            else if(InItemModel==Model->GetPageArea())
            {
                return GetPageArea();
            }
            else
            {
                assert(false);
                return nullptr; 
            }
        }
    );
    BodyLayout->BindModel(Model->GetBodyLayout());


    StatusBarViewModel=NewObject<REditorStatusBarViewModel>(this);
    StatusBarViewModel->BindModel(Model->GetStatusBar());

    NotificationViewModel=NewObject<REditorNotificationViewModel>(this);
    NotificationViewModel->BindModel(Model->GetNotification());

    MenuBarViewModel=NewObject<REditorMenuBarViewModel>(this);
    
}

TSharedObjectPtr<RTexture2D> REditorMainWindowViewModel::GetMainSideBarToggleButtonImage() const
{
    return Model->IsMainSideBarVisible() ? MainSideBarToggleButtonImage_Show : MainSideBarToggleButtonImage_Hide;
}

TSharedObjectPtr<RTexture2D> REditorMainWindowViewModel::GetPanelToggleButtonImage() const
{
    return Model->IsPanelVisible() ? PanelToggleButtonImage_Show : PanelToggleButtonImage_Hide;
}

TSharedObjectPtr<RTexture2D> REditorMainWindowViewModel::GetSecondarySideBarToggleButtonImage() const
{
    return Model->IsSecondarySideBarVisible() ? SecondarySideBarToggleButtonImage_Show : SecondarySideBarToggleButtonImage_Hide;
}

void REditorMainWindowViewModel::ToggleMainSideBar()
{
    Model->ToggleMainSideBarVisible();
}

void REditorMainWindowViewModel::TogglePanel()
{
    Model->TogglePanelVisible();
}

void REditorMainWindowViewModel::HidePanel()
{
    Model->HidePanel();
}

void REditorMainWindowViewModel::ToggleSecondarySideBar()
{
    Model->ToggleSecondarySideBarVisible();
}

bool REditorMainWindowViewModel::IsActivityBarVisible() const
{
    return Model->bShowActivityBar;
}

EActivityBarPostion REditorMainWindowViewModel::GetActivityBarPostion() const
{
    return Model->ActivityBarPostion;
}


int64_t REditorMainWindowViewModel::GetActivityAbilityTabsCount() const
{
    return Model->GetMainSideBar()->GetAbilitysNum();
}

float REditorMainWindowViewModel::GetActivityAbilityTabsHeight() const
{
    return GetActivityAbilityTabsCount()*ActivityButtonHeight;
}

void REditorMainWindowViewModel::OnActivityBarTabClicked(int64_t InIndex)
{
    if(Model->GetMainSideBar()->ActivedAbilityIndex == InIndex)
    {
        Model->GetMainSideBar()->ActivedAbilityIndex = -1;
    }
    else
    {
        Model->GetMainSideBar()->ActivedAbilityIndex = InIndex;
    }
    //OnSelectedActivityAbilityTabIndexChanged.ExecuteIfBound();
}

bool REditorMainWindowViewModel::IsActivityBarTabHighlight(int64_t InIndex) const
{
    return  Model->GetMainSideBar()->ActivedAbilityIndex == InIndex
        && Model->IsMainSideBarVisible();
    
}

TSharedObjectPtr<RTexture2D> REditorMainWindowViewModel::GetActivityAbilityTabIcon(int64_t InIndex) const
{
    return MainSideBar-> GetAbilityIcon(InIndex);
}

TSharedObjectPtr<REditorSplitableLayoutViewModel> REditorMainWindowViewModel::GetBodyLayout()
{
    return BodyLayout;
}

TSharedObjectPtr<REditorSideBarViewModel> REditorMainWindowViewModel::GetMainSideBar() const
{
    return MainSideBar;
}

TSharedObjectPtr<REditorSideBarViewModel> REditorMainWindowViewModel::GetSecondarySideBar() const
{
    return SecondarySideBar;
}

TSharedObjectPtr<REditorPanelViewModel> REditorMainWindowViewModel::GetPanel() const
{
    return PanelViewModel;
}

TSharedObjectPtr<REditorStatusBarViewModel> REditorMainWindowViewModel::GetStatusBar() const
{
    return StatusBarViewModel;
}

TSharedObjectPtr<REditorNotificationViewModel> REditorMainWindowViewModel::GetNotification() const
{
    return NotificationViewModel;
}

TSharedObjectPtr<REditorMenuBarViewModel> REditorMainWindowViewModel::GetMenuBar() const
{
    return MenuBarViewModel;
}
